package com.gitee.cirnochat.login.controller;

import com.gitee.cirnochat.common.annotation.AuthCheck;
import com.gitee.cirnochat.common.model.vo.resp.Result;
import com.gitee.cirnochat.common.utils.RequestUtils;
import com.gitee.cirnochat.login.model.vo.req.LoginDeviceUpdateRequest;
import com.gitee.cirnochat.login.model.vo.resp.LoginDeviceVo;
import com.gitee.cirnochat.login.service.LoginDeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "登录设备相关接口", description = "登录设备相关API")
@RestController
@RequestMapping("/device")
@RequiredArgsConstructor
@Validated
public class LoginDeviceController {
    private final LoginDeviceService loginDeviceService;

    @Operation(summary = "获取登录设备列表", description = "获取用户的登录设备列表")
    @ApiResponse(
            responseCode = "200",
            description = "成功获取登录设备列表",
            content = @Content(array = @ArraySchema(schema = @Schema(implementation = LoginDeviceVo.class)))
    )
    @GetMapping
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @AuthCheck
    public Result<List<LoginDeviceVo>> getLoginDevices(HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        List<LoginDeviceVo> loginDevices = loginDeviceService.getLoginDevicesByUserId(userId);
        return Result.success(loginDevices);
    }

    @Operation(summary = "更新登录设备信息", description = "更新登录设备信息,目前支持更新\"登陆设备名称\"")
    @ApiResponse(
            responseCode = "200",
            description = "成功清除登录设备"
    )
    @PutMapping
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @AuthCheck
    public Result<Void> updateLoginDevice(@RequestBody @Valid LoginDeviceUpdateRequest req, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        loginDeviceService.updateLoginDevice(req, userId);
        return Result.success();
    }


    @Operation(summary = "清除登录设备", description = "清除用户的登录设备;清除该登陆设备后，与之关联的登陆令牌也会被清除")
    @ApiResponse(
            responseCode = "200",
            description = "成功清除登录设备"
    )
    @DeleteMapping("/delete/{deviceId}")
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @AuthCheck
    public Result<Void> deleteLoginDevice(@PathVariable String deviceId, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        loginDeviceService.deleteLoginDeviceByUserId(deviceId,userId);
        return Result.success();
    }


    @DeleteMapping("/deletebatch")
    @Operation(summary = "批量清除", description = "清除用户的登录设备;清除该登陆设备后，与之关联的登陆令牌也会被清除")
    @ApiResponse(
            responseCode = "200",
            description = "成功清除登录设备"
    )
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @AuthCheck
    public Result<Void> deleteBatchLoginDevice(@RequestBody @NotEmpty(message = "设备ID不能为空") List<String> deviceIds, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        loginDeviceService.deleteBatchLoginDevice(deviceIds,userId);
        return Result.success();
    }


    @DeleteMapping("/clear")
    @Operation(summary = "清除除本机以外的登录设备", description = "清除用户的登录设备;清除该登陆设备后，与之关联的登陆令牌也会被清除")
    @ApiResponse(
            responseCode = "200",
            description = "成功清除登录设备"
    )
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @AuthCheck
    public Result<Void> clearLoginDevices(HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        String deviceId = RequestUtils.getDeviceId(request);
        loginDeviceService.deleteAllLoginDevicesExceptCurrentByUserId(deviceId,userId);
        return Result.success();
    }
}
